package com.umfwechat.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.umpay.sp.model.common.ReqMessage;
import com.umpay.sp.model.common.RetCode;
import com.umpay.sp.util.StringUtil;
//import com.umpay.sp.web.util.DateUtil;
//import com.umpay.sp.web.util.PropertyUtil;
//import com.umpay.sp.web.util.RedisUtil;
//import com.umpay.sp.web.util.ReqMessageUtil;
//import com.umpay.sp.web.util.SmsUtil;
import com.umpay.sp.xml.XmlData;
import com.umfwechat.util.ReqMessageUtil;
import com.umfwechat.util.wx.DateUtil;


public class SmsCheckUtil  {
	private static final long serialVersionUID = 1L;
	private static Logger logger = LoggerFactory.getLogger(SmsCheckUtil.class);
	//【1】属性字段
	private static String pbgUrl="";
	private static String pbgcheckUrl="";
	//【2】跳转方法
	
	//【3】初始化方法
	private static void initUrl(){
		String host = PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties", "saasPlat.rest.service.spallyTestMessage", "http://10.10.77.89:57008");
		String service = PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties", "saasPlat.send.service.spallyTestMessage", "/pbg/100003");
		String service2 = PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties", "saasPlat.test.service.spallyTestMessage", "/pbg/100004");
		pbgUrl = host + service;
		pbgcheckUrl=host + service2;
		logger.info("下发短信请求业务网关地址为："+pbgUrl+",校验短信请求地址为："+pbgcheckUrl);
	}
	//【4】前端功能方法
	/**
	 * 任杰
	 * @param 下发验证码短信（标准模式，无个性化参数）
	 * @return
	 * @throws Exception
	 */
	public static Map<String,String> send(String mobileNo,String productId){
		return send(mobileNo,productId,null);
	}
	/**
	 * 任杰
	 * @param 下发验证码短信
	 * @return
	 * @throws Exception
	 */
	public static Map<String,String> send(String mobileNo,String productId,Map<String,String> map){
		initUrl();
		Map<String,String> resmap=new HashMap<String, String>();
//		if(!judgeValidity(mobileNo)){
//			logger.error("下发次数超限");
//			resmap.put("retCode", RetCode.SENDMSGOVERRUN);
//			resmap.put("retMsg", "下发次数超限");
//		}
		RandomCode VerifyCode = new RandomCode();
		String verifyCode = VerifyCode.getSix();
		String trace = VerifyCode.getSixTeen();
		logger.info("给商户"+mobileNo+"下发的验证码是："+verifyCode);
		ReqMessage req=makesendreq(mobileNo,trace,verifyCode,productId,map);
		//下发给客户的验证码
		Map<String, Object> result;
		try {
			result = SmsUtil.sendMsg(req,pbgUrl);//接收发送验证码返回结果
			System.out.println("验证码的发送结果是："+result);
			String retcode = (String)result.get(XmlData.RETCODE);			//获取retcode
			if(RetCode.SUCCESS.equals(retcode)){      						//短信验证码下发成功，将下列放入session
				resmap.put("retCode", "0000");
				resmap.put("retMsg", "短信发送成功");
				resmap.put("smsVerifyTrace", trace);
				resmap.put("verifyCode", verifyCode);
				resmap.put("mobileNo", mobileNo);
			}else{
				resmap.put("retCode", retcode);
				resmap.put("retMsg", (String)result.get(XmlData.RETMSG));
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			resmap.put("retCode", RetCode.UNKNOWN);
			resmap.put("retMsg", e.getMessage());
		}
		return resmap;
	} 
	/**
	 * 任杰
	 * @param 校验短信验证码
	 * @return
	 * @throws Exception
	 */	
	public static  Map<String,String> checksms(String mobileNo,String trace,String verifyCode) {
		initUrl();
		Map<String,String> resmap=new HashMap<String, String>();
		ReqMessage req = makecheckreq(mobileNo,trace,verifyCode);  
		Map<String, Object> result;
		try {
				result = SmsUtil.testMsg(req,pbgcheckUrl);
			if(!RetCode.SUCCESS.equals(StringUtil.trim((result.get("retCode"))))){  		//判断验证验证码是否通过     
				resmap.put("retCode", StringUtil.trim((result.get("retCode"))));
				resmap.put("retMsg", StringUtil.trim((result.get("retMsg"))));
			}else{
				resmap.put("retCode", "0000");
				resmap.put("retMsg", "短信校验成功");
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			resmap.put("retCode", RetCode.UNKNOWN);
			resmap.put("retMsg", e.getMessage());
		}
		return resmap;
	}
	/**
	 * 判断手机号是否超过当前日期的错误次数
	 * @param mobileNo
	 * @return
	 */
//	public static boolean judgeValidity(String mobileNo) {
//		String key=DateUtil.date8(new Date())+mobileNo;//拼接记录手机号当日错误数字的key值
//		String tip=RedisUtil.getStr(key);
//		if (null==tip) {
//			//TODO 如果未取到对应手机号的
//			RedisUtil.setString(key, String.valueOf(1));
//			return true;
//		}
//		int errTime=Integer.parseInt(tip);
//		if (errTime<=10&&errTime>=0) {
//			RedisUtil.setString(key, String.valueOf(errTime+1));
//			return true;
//		} else {
//			return false;
//		}
//	}
	//【5】内部子功能方法
	public static ReqMessage makesendreq(String mobileNo,String trace,String verifyCode,String productId,Map<String,String> map){
		ReqMessage req = ReqMessageUtil.makeSpReq();
		req.put(XmlData.MOBILEID, mobileNo);							//发送验证码必传
		req.put(XmlData.PRODUCTID,productId);							//模板编号
		req.put("validTime", "600");									//验证码有效期
		req.put("maxErrorCount", "3");									//最大错误次数
		req.put(XmlData.INSTID,"20000009");								//内部机构号?
		req.put(XmlData.TRACE, trace);									//流水号
		req.put(XmlData.ORDERDATE, DateUtil.getDateTime());							//订单日期?
		req.put(XmlData.MERNAME, "联动1");								//商户名称?
		req.put(XmlData.AMOUNT, "10");									//金额?
		req.put(XmlData.VERIFYCODE,verifyCode);
		req.put("smsFunCode","VERIFYCODESMS");
		if(map!=null){
			for(String key:map.keySet()){
				req.put(key, StringUtil.trim(map.get(key)));
			}
		}
		return req;
	}
	
	public static ReqMessage makecheckreq(String mobileNo,String trace,String verifyCode){
		ReqMessage req = ReqMessageUtil.makeSpReq();
		req.put(XmlData.MOBILENO, mobileNo);
		req.put(XmlData.MOBILEID, mobileNo);
		req.put(XmlData.TRACE, trace);
		req.put(XmlData.INSTID, "20000009");
		req.put(XmlData.VERIFYCODE,verifyCode);
		return req;
	}
	//【6】get&&set
	public static String getPbgUrl() {
		return pbgUrl;
	}
	public static void setPbgUrl(String pbgUrl) {
		SmsCheckUtil.pbgUrl = pbgUrl;
	}
	public static String getPbgcheckUrl() {
		return pbgcheckUrl;
	}
	public static void setPbgcheckUrl(String pbgcheckUrl) {
		SmsCheckUtil.pbgcheckUrl = pbgcheckUrl;
	}
}
